﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ShopOnline
{
    /// <summary>
    /// TODO:Put a description here.
    /// </summary>
    [Serializable]
    public class DomainRec
    {
        #region Private Members
        private Int64 m_id = 0;
        private String m_domain = String.Empty;
        private String m_password = String.Empty;
        private String m_name = String.Empty;
        private String m_address = String.Empty;
        private String m_email = String.Empty;
        private DateTime m_createDate = DateTime.MinValue;
        private DateTime m_expiredDate = DateTime.MinValue;
        private int m_status =-1;
        private Int64 m_themeId = 0;

        private String m_themeName = String.Empty;

        private String m_themeLink = String.Empty;

        private int m_themeStatus = -1;

        
        #endregion

        #region Public Properties
        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String domain
        {
            get { return m_domain; }
            set { m_domain = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String password
        {
            get { return m_password; }
            set { m_password = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String address
        {
            get { return m_address; }
            set { m_address = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public String email
        {
            get { return m_email; }
            set { m_email = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public DateTime createDate
        {
            get { return m_createDate; }
            set { m_createDate = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public DateTime expiredDate
        {
            get { return m_expiredDate; }
            set { m_expiredDate = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public int status
        {
            get { return m_status; }
            set { m_status = value; }
        }

        /// <summary>
        /// TODO:Put a description here.
        /// </summary>
        public Int64 themeId
        {
            get { return m_themeId; }
            set { m_themeId = value; }
        }


        public String ThemeName
        {
            get { return m_themeName; }
            set { m_themeName = value; }
        }

        public String ThemeLink
        {
            get { return m_themeLink; }
            set { m_themeLink = value; }
        }

        public int ThemeStatus
        {
            get { return m_themeStatus; }
            set { m_themeStatus = value; }
        }
        #endregion

        #region  Properties Const
        private const string ConnectName = "ShopOnline";
        private const string Folderlogs = "Domain";
        #endregion

        #region Store procedure
        private String usp_GetSingle = "usp_GetSingle_Domain";
        private String usp_GetAll_Domain = "usp_GetAll_Domain";
        private String usp_ResetPass_Domain = "usp_ResetPass_Domain";
        private String usp_Update_Domain = "usp_Update_Domain";
        private String usp_Add_Domain = "usp_Add_Domain";
        #endregion

        #region Init
        public DomainRec() { }

        public DomainRec(Int64 id, String domain, String password, String name, String address, String email, DateTime createDate, DateTime expiredDate, int status, Int64 themeId)
        {
            m_id = id;
            m_domain = domain;
            m_password = password;
            m_name = name;
            m_address = address;
            m_email = email;
            m_createDate = createDate;
            m_expiredDate = expiredDate;
            m_status = status;
            m_themeId = themeId;
        }
        #endregion

        #region Call Store Procedure
        public DomainRec GetSingle(string t_domain, string t_password)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetSingle - Domain:" + t_domain;
            try
            {
                DomainRec objDomain = new DomainRec();

                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetSingle, t_domain, MD5.Encrypt(t_password));

                if (dsResult.Tables[0].Rows.Count > 0)
                {
                    objDomain.id = Convert.ToInt64(dsResult.Tables[0].Rows[0]["id"].ToString());
                    if (!dsResult.Tables[0].Rows[0].IsNull("name"))
                    {
                        objDomain.name = dsResult.Tables[0].Rows[0]["name"].ToString();
                    }

                    objDomain.domain = dsResult.Tables[0].Rows[0]["domain"].ToString();
                    if (!dsResult.Tables[0].Rows[0].IsNull("address"))
                    {
                        objDomain.address = dsResult.Tables[0].Rows[0]["address"].ToString();
                    }

                    objDomain.email = dsResult.Tables[0].Rows[0]["email"].ToString();
                    objDomain.createDate = Convert.ToDateTime(dsResult.Tables[0].Rows[0]["createDate"].ToString());
                    objDomain.themeId = Convert.ToInt64(dsResult.Tables[0].Rows[0]["themeId"].ToString());
                    objDomain.status = Convert.ToInt16(dsResult.Tables[0].Rows[0]["status"].ToString());

                    if (!dsResult.Tables[0].Rows[0].IsNull("expiredDate"))
                    {
                        objDomain.expiredDate = Convert.ToDateTime(dsResult.Tables[0].Rows[0]["expiredDate"].ToString());
                    }

                    objDomain.ThemeLink = dsResult.Tables[0].Rows[0]["link"].ToString();
                    objDomain.ThemeName = dsResult.Tables[0].Rows[0]["ThemeName"].ToString();
                    objDomain.ThemeStatus = Convert.ToInt16(dsResult.Tables[0].Rows[0]["ThemeStatus"].ToString());
                }
                else
                {
                    iFlagError = 1;
                    sMgsError = "Empty";
                    objDomain = null;
                }

                return objDomain;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return null;
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        public List<DomainRec> GetAll(int PageSize, int PageNumber, ref int Total)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "GetAll - PageSize:" + PageSize + ",PageNumber: " + PageNumber;
            try
            {
                List<DomainRec> records = new List<DomainRec>();
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_GetAll_Domain, PageSize, PageNumber);

                #region Default variables
                Int64 id = 0;
                String domain = String.Empty;
                String password = String.Empty;
                String name = String.Empty;
                String address = String.Empty;
                String email = String.Empty;
                DateTime createdate = DateTime.MinValue;
                DateTime expireddate = DateTime.MinValue;
                int status = -1;
                Int64 themeid = 0;
                #endregion
                if (dsResult.Tables.Count == 2)
                {
                    foreach (DataRow row in dsResult.Tables[0].Rows)
                    {
                        if (row.IsNull("id") == false)
                            id = (Int64)row["id"];
                        if (row.IsNull("domain") == false)
                            domain = row["domain"].ToString();
                        if (row.IsNull("name") == false)
                            name = row["name"].ToString();
                        if (row.IsNull("address") == false)
                            address = row["address"].ToString();
                        if (row.IsNull("email") == false)
                            email = row["email"].ToString();
                        if (row.IsNull("createDate") == false)
                            createdate = (DateTime)row["createDate"];
                        if (row.IsNull("expiredDate") == false)
                            expireddate = (DateTime)row["expiredDate"];
                        if (row.IsNull("status") == false)
                            status = (Byte)row["status"];
                        if (row.IsNull("themeId") == false)
                            themeid = (Int64)row["themeId"];
                        records.Add(new DomainRec(id, domain, password, name, address, email, createdate, expireddate, status, themeid));
                    }

                    Total = (int)dsResult.Tables[1].Rows[0][0];
                }



                if (records.Count <= 0) {
                    iFlagError = 1;
                    sMgsError = "Empty";
                }

                return records;
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new List<DomainRec>();
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }

        }

        /// <summary>
        /// ResetNewPass
        /// _Other Result = String NewPass
        /// </summary>
        /// <returns>Flag|Description|NewPass</returns>
        public sOtherResult ResetNewPass(Int64 DomainID)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "ResetNewPass - DomainID:" + DomainID;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_ResetPass_Domain, DomainID);

                int iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                string sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                if (iFlag == 0)
                {
                    return new sOtherResult(true, sDescription, dsResult.Tables[0].Rows[0]["NewPass"].ToString());
                }
                else {
                    iFlagError = 1;
                    sMgsError = sDescription;
                    return new sOtherResult(false, sDescription);
                }
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new sOtherResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ObjectResult UpdateDomain(Int64 DomainID,string DomainName, string DomainAddress , string DomainEmail, int status)
        {
            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "UpdateDomain - DomainID:" + DomainID;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Update_Domain, DomainID,DomainName,DomainAddress,DomainEmail,status);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, false, sLog, Folderlogs, "", sMgsError);
            }
        }

        public ObjectResult AddDomain(Int64 t_DomainID, string t_Domain, string t_PassWord, string t_DomainName, string t_DomainAddress, string t_DomainEmail)
        {

            int iFlagError = 0;
            string sMgsError = "thành công";
            string sLog = "AddDomain - DomainID:" + t_DomainID + ",Domain:" + t_Domain;
            try
            {
                SqlDataProvider objsql = new SqlDataProvider(ConnectName);
                DataSet dsResult = objsql.ExecuteQueryStoreProc(usp_Add_Domain, t_DomainID, t_Domain, MD5.Encrypt(t_PassWord), t_DomainName, t_DomainAddress, t_DomainEmail);

                int iFlag = -1;
                string sDescription = string.Empty;

                iFlag = (int)dsResult.Tables[0].Rows[0]["Flag"];
                sDescription = dsResult.Tables[0].Rows[0]["Description"].ToString();
                bool bFlag = true;
                if (iFlag != 0)
                {
                    bFlag = false;
                    iFlagError = 1;
                    sMgsError = sDescription;
                }

                return new ObjectResult(bFlag, sDescription);
            }
            catch (Exception ex)
            {
                iFlagError = -1;
                sMgsError = ex.ToString();
                return new ObjectResult(false, "Lỗi không xác định");
            }
            finally
            {
                WriteLog.WriteLogWithFlag(iFlagError, true, sLog, Folderlogs, "", sMgsError);
            }

        }
        #endregion
        





        
    }

}
